{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Leave-One-Out Cross-Validation"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "# yfinance is used to fetch data \n",
        "import yfinance as yf\n",
        "yf.pdr_override()"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:09.189Z",
          "iopub.execute_input": "2021-07-08T01:08:09.193Z",
          "iopub.status.idle": "2021-07-08T01:08:09.687Z",
          "shell.execute_reply": "2021-07-08T01:08:09.678Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# input\n",
        "symbol = 'AMD'\n",
        "start = '2014-01-01'\n",
        "end = '2018-08-27'\n",
        "\n",
        "# Read data \n",
        "dataset = yf.download(symbol,start,end)\n",
        "\n",
        "# View Columns\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[*********************100%***********************]  1 of 1 completed\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 2,
          "data": {
            "text/plain": "            Open  High   Low  Close  Adj Close    Volume\nDate                                                    \n2014-01-02  3.85  3.98  3.84   3.95       3.95  20548400\n2014-01-03  3.98  4.00  3.88   4.00       4.00  22887200\n2014-01-06  4.01  4.18  3.99   4.13       4.13  42398300\n2014-01-07  4.19  4.25  4.11   4.18       4.18  42932100\n2014-01-08  4.23  4.26  4.14   4.18       4.18  30678700",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2014-01-02</th>\n      <td>3.85</td>\n      <td>3.98</td>\n      <td>3.84</td>\n      <td>3.95</td>\n      <td>3.95</td>\n      <td>20548400</td>\n    </tr>\n    <tr>\n      <th>2014-01-03</th>\n      <td>3.98</td>\n      <td>4.00</td>\n      <td>3.88</td>\n      <td>4.00</td>\n      <td>4.00</td>\n      <td>22887200</td>\n    </tr>\n    <tr>\n      <th>2014-01-06</th>\n      <td>4.01</td>\n      <td>4.18</td>\n      <td>3.99</td>\n      <td>4.13</td>\n      <td>4.13</td>\n      <td>42398300</td>\n    </tr>\n    <tr>\n      <th>2014-01-07</th>\n      <td>4.19</td>\n      <td>4.25</td>\n      <td>4.11</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>42932100</td>\n    </tr>\n    <tr>\n      <th>2014-01-08</th>\n      <td>4.23</td>\n      <td>4.26</td>\n      <td>4.14</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>30678700</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:09.695Z",
          "iopub.execute_input": "2021-07-08T01:08:09.699Z",
          "iopub.status.idle": "2021-07-08T01:08:10.355Z",
          "shell.execute_reply": "2021-07-08T01:08:10.451Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset.tail()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 3,
          "data": {
            "text/plain": "                 Open   High        Low      Close  Adj Close     Volume\nDate                                                                    \n2018-08-20  19.790001  20.08  19.350000  19.980000  19.980000   62983200\n2018-08-21  19.980000  20.42  19.860001  20.400000  20.400000   55629000\n2018-08-22  20.280001  20.92  20.209999  20.900000  20.900000   62002700\n2018-08-23  21.190001  22.32  21.139999  22.290001  22.290001  113444100\n2018-08-24  22.910000  24.00  22.670000  23.980000  23.980000  164328200",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2018-08-20</th>\n      <td>19.790001</td>\n      <td>20.08</td>\n      <td>19.350000</td>\n      <td>19.980000</td>\n      <td>19.980000</td>\n      <td>62983200</td>\n    </tr>\n    <tr>\n      <th>2018-08-21</th>\n      <td>19.980000</td>\n      <td>20.42</td>\n      <td>19.860001</td>\n      <td>20.400000</td>\n      <td>20.400000</td>\n      <td>55629000</td>\n    </tr>\n    <tr>\n      <th>2018-08-22</th>\n      <td>20.280001</td>\n      <td>20.92</td>\n      <td>20.209999</td>\n      <td>20.900000</td>\n      <td>20.900000</td>\n      <td>62002700</td>\n    </tr>\n    <tr>\n      <th>2018-08-23</th>\n      <td>21.190001</td>\n      <td>22.32</td>\n      <td>21.139999</td>\n      <td>22.290001</td>\n      <td>22.290001</td>\n      <td>113444100</td>\n    </tr>\n    <tr>\n      <th>2018-08-24</th>\n      <td>22.910000</td>\n      <td>24.00</td>\n      <td>22.670000</td>\n      <td>23.980000</td>\n      <td>23.980000</td>\n      <td>164328200</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 3,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:10.363Z",
          "iopub.execute_input": "2021-07-08T01:08:10.369Z",
          "iopub.status.idle": "2021-07-08T01:08:10.381Z",
          "shell.execute_reply": "2021-07-08T01:08:10.455Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = dataset.iloc[ : , 0:4].values\n",
        "y = dataset.iloc[ : , 4].values"
      ],
      "outputs": [],
      "execution_count": 4,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:10.388Z",
          "iopub.execute_input": "2021-07-08T01:08:10.393Z",
          "iopub.status.idle": "2021-07-08T01:08:10.402Z",
          "shell.execute_reply": "2021-07-08T01:08:10.457Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(X.shape)\n",
        "print(y.shape)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(1171, 4)\n",
            "(1171,)\n"
          ]
        }
      ],
      "execution_count": 5,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:10.409Z",
          "iopub.execute_input": "2021-07-08T01:08:10.413Z",
          "iopub.status.idle": "2021-07-08T01:08:10.423Z",
          "shell.execute_reply": "2021-07-08T01:08:10.460Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.model_selection import LeaveOneOut\n",
        "from sklearn.model_selection import cross_val_score\n",
        "from sklearn.linear_model import LinearRegression"
      ],
      "outputs": [],
      "execution_count": 6,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:10.430Z",
          "iopub.execute_input": "2021-07-08T01:08:10.434Z",
          "iopub.status.idle": "2021-07-08T01:08:10.777Z",
          "shell.execute_reply": "2021-07-08T01:08:10.769Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#define cross-validation method to use\n",
        "cv = LeaveOneOut()\n",
        "\n",
        "# Build multiple linear regression model\n",
        "model = LinearRegression()\n",
        "\n",
        "# Use LOOCV to evaluate model\n",
        "scores = cross_val_score(model, X, y, scoring='neg_mean_absolute_error',\n",
        "                         cv=cv, n_jobs=-1)\n"
      ],
      "outputs": [],
      "execution_count": 7,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:10.787Z",
          "iopub.execute_input": "2021-07-08T01:08:10.794Z",
          "iopub.status.idle": "2021-07-08T01:08:12.432Z",
          "shell.execute_reply": "2021-07-08T01:08:12.534Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Print Mean Absolute Error (MAE)\n",
        "np.mean(np.absolute(scores))"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 8,
          "data": {
            "text/plain": "3.2752053275555964e-14"
          },
          "metadata": {}
        }
      ],
      "execution_count": 8,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:12.438Z",
          "iopub.execute_input": "2021-07-08T01:08:12.442Z",
          "iopub.status.idle": "2021-07-08T01:08:12.450Z",
          "shell.execute_reply": "2021-07-08T01:08:12.537Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Print Root Mean Squared Error (RMSE)\n",
        "np.sqrt(np.mean(np.absolute(scores)))"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 9,
          "data": {
            "text/plain": "1.8097528360402135e-07"
          },
          "metadata": {}
        }
      ],
      "execution_count": 9,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:12.455Z",
          "iopub.execute_input": "2021-07-08T01:08:12.460Z",
          "iopub.status.idle": "2021-07-08T01:08:12.468Z",
          "shell.execute_reply": "2021-07-08T01:08:12.540Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model.fit(X,y)\n",
        "model.score(X, y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 10,
          "data": {
            "text/plain": "1.0"
          },
          "metadata": {}
        }
      ],
      "execution_count": 10,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:12.474Z",
          "iopub.execute_input": "2021-07-08T01:08:12.478Z",
          "iopub.status.idle": "2021-07-08T01:08:12.490Z",
          "shell.execute_reply": "2021-07-08T01:08:12.542Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Splitting the dataset into the Training set and Test set\n",
        "from sklearn.model_selection import train_test_split\n",
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)\n",
        "print ('Train set:', X_train.shape,  y_train.shape)\n",
        "print ('Test set:', X_test.shape,  y_test.shape)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Train set: (878, 4) (878,)\n",
            "Test set: (293, 4) (293,)\n"
          ]
        }
      ],
      "execution_count": 11,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:12.495Z",
          "iopub.execute_input": "2021-07-08T01:08:12.501Z",
          "iopub.status.idle": "2021-07-08T01:08:12.511Z",
          "shell.execute_reply": "2021-07-08T01:08:12.544Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#evaluate model performance\n",
        "scores = cross_val_score(model, X_test, y_test, scoring='neg_mean_absolute_error',\n",
        "                         cv=cv, n_jobs=-1)"
      ],
      "outputs": [],
      "execution_count": 12,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:12.518Z",
          "iopub.execute_input": "2021-07-08T01:08:12.522Z",
          "iopub.status.idle": "2021-07-08T01:08:12.651Z",
          "shell.execute_reply": "2021-07-08T01:08:12.644Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Print Mean Absolute Error (MAE)\n",
        "np.mean(np.absolute(scores))"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 13,
          "data": {
            "text/plain": "1.5486284988542712e-14"
          },
          "metadata": {}
        }
      ],
      "execution_count": 13,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:12.657Z",
          "iopub.execute_input": "2021-07-08T01:08:12.660Z",
          "iopub.status.idle": "2021-07-08T01:08:12.668Z",
          "shell.execute_reply": "2021-07-08T01:08:12.710Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Print Root Mean Squared Error (RMSE)\n",
        "np.sqrt(np.mean(np.absolute(scores)))"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 14,
          "data": {
            "text/plain": "1.2444390297858192e-07"
          },
          "metadata": {}
        }
      ],
      "execution_count": 14,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:12.673Z",
          "iopub.execute_input": "2021-07-08T01:08:12.676Z",
          "iopub.status.idle": "2021-07-08T01:08:12.685Z",
          "shell.execute_reply": "2021-07-08T01:08:12.713Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model.fit(X,y)\n",
        "model.score(X, y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 15,
          "data": {
            "text/plain": "1.0"
          },
          "metadata": {}
        }
      ],
      "execution_count": 15,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-07-08T01:08:12.692Z",
          "iopub.execute_input": "2021-07-08T01:08:12.695Z",
          "iopub.status.idle": "2021-07-08T01:08:12.702Z",
          "shell.execute_reply": "2021-07-08T01:08:12.715Z"
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.6.12",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "kernelspec": {
      "argv": [
        "C:/Users/Tin Hang/Anaconda3\\python.exe",
        "-m",
        "ipykernel_launcher",
        "-f",
        "{connection_file}"
      ],
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}